package org.example.erp.controller;

import org.example.erp.domain.ProductBom;
import org.example.erp.domain.ProductMaterialManagement;
import org.example.erp.mapper.ProductBomMapper;
import org.example.erp.service.ProductBomService;
import org.example.erp.service.impl.ProductBomServiceImpl;
import org.example.erp.service.impl.ProductMaterialManagementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ProductMaterialManagementController {
    @Autowired
    private ProductMaterialManagementServiceImpl productMaterialManagementService;

    @Autowired
    private ProductBomServiceImpl productBomService;

   @GetMapping("/materialManagement/find")
   public DataResult find(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                          @RequestParam (required = false)String productName,
                          @RequestParam (required = false)String materialName,
                          @RequestParam (required = false)String materialProductCode,
                          @RequestParam (required = false) Integer materialId
   ){
       return DataResult.success(productMaterialManagementService.find(currentPage,pageSize,productName,materialName,materialProductCode,materialId)) ;
   }


   @PostMapping("/materialManagement/insert")
    public DataResult insert(@RequestBody ProductMaterialManagement productMaterialManagement ){
       System.out.println("添加");
       productMaterialManagementService.save(productMaterialManagement);
       return DataResult.success();
   }


   @DeleteMapping("/materialManagement/delete")
    public  DataResult delete(@RequestBody List<Integer> logIds){
       productMaterialManagementService.removeByIds(logIds);
       return DataResult.success();
   }


   @PutMapping("/materialManagement/update")
    public DataResult update(@RequestBody ProductMaterialManagement productMaterialManagement){
       productMaterialManagementService.updateById(productMaterialManagement);
       return DataResult.success();
   }


   @GetMapping("/boms/find")
    public DataResult findboms(
           @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
           @RequestParam (required = false)String materialProductCode
   ){
       return DataResult.success(productMaterialManagementService.findboms(currentPage,pageSize,materialProductCode));
   }

   @PostMapping("/boms/insert")
   public DataResult insertboms(@RequestBody List<ProductBom> list) {
       // 调用服务层的批量保存方法
       productBomService.saveBatch(list);
       return DataResult.success();
   }

   @PutMapping("/boms/update")
    public DataResult updateboms(@RequestBody ProductBom productBom){
       productBomService.updateById(productBom);
       return DataResult.success();
   }

   @DeleteMapping("/boms/delete")
    public DataResult bomsdelete(@RequestBody List<Integer> logIds){
       productBomService.removeByIds(logIds);
       return DataResult.success();
   }

   // 查询所有产品信息
   @GetMapping("/materialManagement/findProduct")
   public DataResult findProducts(){
       return DataResult.success(productMaterialManagementService.findProducts());
   }
}

